package ro.myApp.security.client;

import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.intercept.BypassInterceptors;

import ro.myApp.client.BaseView;
import ro.myApp.security.dto.RoleFindAllFiltered;
import ro.myApp.security.dto.RoleFindUniqueFiltered;
import ro.myApp.security.dto.UserFindAllFiltered;
import ro.myApp.security.dto.UserSaveMessage;
import ro.myApp.security.entities.Role;
import ro.myApp.security.entities.User;


@Name("requests")
@BypassInterceptors
@Scope(ScopeType.PAGE)
public class Requests extends BaseView {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4871766915223321114L;

	private List<User> usersList;
	
	private User instance;
	
	private Long roleId;
	
	public List<User> getUsersList (){
		if (null==usersList){
			UserFindAllFiltered find = new UserFindAllFiltered();
			find.setWithInterests(true);
			usersList = sendMessage(find);
		}
		return usersList;
	}
	
	public Long getRoleId() {
		return roleId;
	}
	
	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}
	
	public void edit (User user){
		instance=user;
	}
	
	public void save (){
		RoleFindUniqueFiltered findRole = new RoleFindUniqueFiltered();
		findRole.setId(roleId);
		Role role=sendMessage(findRole);
		instance.setRole(role);
		instance.setInterests(null);
		sendMessage(new UserSaveMessage(instance));
		
		usersList=null;
		cancel();
	}
	
	public void cancel (){
		instance=null;
	}
	
	public User getInstance() {
		return instance;
	}
	public void setInstance(User instance) {
		this.instance = instance;
	}
	
	public List<Role> getAllRoles(){
		return sendMessage(new RoleFindAllFiltered());
	}
}
